﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ForzaGT3.Data;
using ForzaGT3.Areas.Admin.Models;
using ForzaGT3.Common.Configuration;

namespace ForzaGT3.Areas.Admin.Controllers
{
    public partial class GalleryController : Controller
    {
        //
        // GET: /Admin/Gallery/

        public virtual ActionResult Index()
        {
            // Get paging values from the query string
            int pageNum = CustomGrid.QueryParams.getPageNumber("data", Request.QueryString);
            int pageSize = CustomGrid.QueryParams.getPageSize("data", Request.QueryString);

            // Get sorting values from query string
            string sort = CustomGrid.QueryParams.getSortField("data", Request.QueryString, new Series(), "CreatedDate");
            string stype = CustomGrid.QueryParams.getSortType("data", Request.QueryString, "desc");

            // Get filter where conditions and values from the query string            
            List<object> whereValues = new List<object>();
            string whereConditions = "";
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Title", Request.QueryString, whereValues);
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Url", Request.QueryString, whereValues);

            // Get data from database
            int totalRecords;
            IQueryable<Gallery> data = Gallery.AdminGetAll(whereConditions, whereValues, sort, stype, pageNum, pageSize, out totalRecords);

            // Count the total number of records
            ViewBag.totalRecords = totalRecords;
            ViewBag.CurrentPageName = "Gallery";

            return View(data.ToList());
        }

        public virtual ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult Add(GalleryModel model)
        {
            if (ModelState.IsValid)
            {
                Gallery data = new Gallery();
                data.GalleryId = Guid.NewGuid();
                data.Title = model.Title;
                data.Caption = model.Caption;
                if (model.Image != null)
                {
                    data.Url = SiteSettings.GalleryImageFolder + model.Image.FileName;
                    model.Image.SaveAs(Server.MapPath("~" + SiteSettings.GalleryImageFolder) + model.Image.FileName);
                }

                if (model.ThumbImage != null)
                {
                    data.ThumbUrl = SiteSettings.GalleryImageFolder + model.ThumbImage.FileName;
                    model.ThumbImage.SaveAs(Server.MapPath("~" + SiteSettings.GalleryImageFolder) + model.ThumbImage.FileName);
                }
                data.CreatedDate = DateTime.Now;
                data.CreatedBy = HttpContext.User.Identity.Name;
                data.Insert();

                return RedirectToAction(MVC.Admin.Gallery.Index());
            }
            return View(model);
        }

        public virtual ActionResult Edit(Guid Id)
        {
            Gallery data = Gallery.GetById(Id);
            GalleryModel model = new GalleryModel();
            if (data != null)
            {
                model.Id = data.GalleryId;
                model.Title = data.Title;
                model.Caption = data.Caption;
                model.Url = data.Url;
                model.ThumbUrl = data.ThumbUrl;
            }
            return View(model);
        }

        [HttpPost]
        public virtual ActionResult Edit(GalleryModel model)
        {
            Gallery data = Gallery.GetById(model.Id);
            if (data != null)
            {
                data.Title = model.Title;
                data.Caption = model.Caption;
                if (model.Image != null)
                {
                    data.Url = SiteSettings.GalleryImageFolder + model.Image.FileName;
                    model.Image.SaveAs(Server.MapPath("~" + SiteSettings.GalleryImageFolder) + model.Image.FileName);
                }
                if (model.ThumbImage != null)
                {
                    data.ThumbUrl = SiteSettings.GalleryImageFolder + model.ThumbImage.FileName;
                    model.Image.SaveAs(Server.MapPath("~" + SiteSettings.GalleryImageFolder) + model.ThumbImage.FileName);
                }
                data.ModifiedDate = DateTime.Now;
                data.ModifiedBy = HttpContext.User.Identity.Name;
                data.Update();

                return RedirectToAction(MVC.Admin.Gallery.Index());
            }

            return View(model);
        }

        public virtual ActionResult Delete(Guid Id)
        {
            Gallery data = Gallery.GetById(Id);
            if (data != null)
            {
                data.IsDeleted = true;
                data.Update();
            }

            return RedirectToAction(MVC.Admin.Gallery.Index());
        }
    }
}
